//加载系统头文件base.h
#include <base.h>
#include "display.h"
#include "button.h"
#include "calc.h"

#define EXP_LEN   128

int buttonClickId;     //记录按键按下的id,手机C不支持多点触控，按下和松开为同一键值。
char exp[EXP_LEN];     //运算表达式
button btn[BUTTON_N];  //按键数量

//入口函数，程序启动时开始执行
int init()
{
    //
    setscrsize(1080, 1920);
    printf("按键占用内存：%d byte\n",sizeof(button)*BUTTON_N);
    cls(237,237,237);
    dtext("计算器",20,20,0,0,0,0,2);
    ref(0,0,SCRW,SCRH);
    buttonCreate(&btn[0] , "0", '0', BUTTON_D*2+BUTTON_W  ,SCRH-BUTTON_H  -BUTTON_D  ,BUTTON_W,BUTTON_H,0xFBFBFB,0xFBFBFB,0xA8A8A8,0xDBDBDB,0,0,0);
    buttonCreate(&btn[1] , "1", '1', BUTTON_D,             SCRH-BUTTON_H*2-BUTTON_D*2,BUTTON_W,BUTTON_H,0xFBFBFB,0xFBFBFB,0xA8A8A8,0xDBDBDB,0,0,0);
    buttonCreate(&btn[2] , "2", '2', BUTTON_D*2+BUTTON_W  ,SCRH-BUTTON_H*2-BUTTON_D*2,BUTTON_W,BUTTON_H,0xFBFBFB,0xFBFBFB,0xA8A8A8,0xDBDBDB,0,0,0);
    buttonCreate(&btn[3] , "3", '3', BUTTON_D*3+BUTTON_W*2,SCRH-BUTTON_H*2-BUTTON_D*2,BUTTON_W,BUTTON_H,0xFBFBFB,0xFBFBFB,0xA8A8A8,0xDBDBDB,0,0,0);
    buttonCreate(&btn[4] , "4", '4', BUTTON_D,             SCRH-BUTTON_H*3-BUTTON_D*3,BUTTON_W,BUTTON_H,0xFBFBFB,0xFBFBFB,0xA8A8A8,0xDBDBDB,0,0,0);
    buttonCreate(&btn[5] , "5", '5', BUTTON_D*2+BUTTON_W  ,SCRH-BUTTON_H*3-BUTTON_D*3,BUTTON_W,BUTTON_H,0xFBFBFB,0xFBFBFB,0xA8A8A8,0xDBDBDB,0,0,0);
    buttonCreate(&btn[6] , "6", '6', BUTTON_D*3+BUTTON_W*2,SCRH-BUTTON_H*3-BUTTON_D*3,BUTTON_W,BUTTON_H,0xFBFBFB,0xFBFBFB,0xA8A8A8,0xDBDBDB,0,0,0);
    buttonCreate(&btn[7] , "7", '7', BUTTON_D,             SCRH-BUTTON_H*4-BUTTON_D*4,BUTTON_W,BUTTON_H,0xFBFBFB,0xFBFBFB,0xA8A8A8,0xDBDBDB,0,0,0);
    buttonCreate(&btn[8] , "8", '8', BUTTON_D*2+BUTTON_W  ,SCRH-BUTTON_H*4-BUTTON_D*4,BUTTON_W,BUTTON_H,0xFBFBFB,0xFBFBFB,0xA8A8A8,0xDBDBDB,0,0,0);
    buttonCreate(&btn[9] , "9", '9', BUTTON_D*3+BUTTON_W*2,SCRH-BUTTON_H*4-BUTTON_D*4,BUTTON_W,BUTTON_H,0xFBFBFB,0xFBFBFB,0xA8A8A8,0xDBDBDB,0,0,0);
    buttonCreate(&btn[10], "±",  0 , BUTTON_D             ,SCRH-BUTTON_H  -BUTTON_D  ,BUTTON_W,BUTTON_H,0xF4F4F4,0xF4F4F4,0xA8A8A8,0xDBDBDB,0,0,0);
    buttonCreate(&btn[11], "=", '=', BUTTON_D*3+BUTTON_W*2,SCRH-BUTTON_H  -BUTTON_D  ,BUTTON_W,BUTTON_H,0xF4F4F4,0xF4F4F4,0x289482,0x33C1A9,0,0xffffff,0);
    buttonCreate(&btn[12], "(", '(', BUTTON_D             ,SCRH-BUTTON_H*5-BUTTON_D*5,BUTTON_W,BUTTON_H,0xF4F4F4,0xF4F4F4,0xA8A8A8,0xDBDBDB,0,0,0);
    buttonCreate(&btn[13], ")", ')', BUTTON_D*2+BUTTON_W  ,SCRH-BUTTON_H*5-BUTTON_D*5,BUTTON_W,BUTTON_H,0xF4F4F4,0xF4F4F4,0xA8A8A8,0xDBDBDB,0,0,0);
    buttonCreate(&btn[14], "<-",'D', BUTTON_D*3+BUTTON_W*2,SCRH-BUTTON_H*5-BUTTON_D*5,BUTTON_W,BUTTON_H,0xF4F4F4,0xF4F4F4,0xA8A8A8,0xDBDBDB,0,0,0);
    buttonCreate(&btn[15], "+", '+', BUTTON_D*4+BUTTON_W*3,SCRH-BUTTON_H  -BUTTON_D  ,BUTTON_W,BUTTON_H,0xF4F4F4,0xF4F4F4,0x289482,0x33C1A9,0,0xffffff,0);
    buttonCreate(&btn[16], "-", '-', BUTTON_D*4+BUTTON_W*3,SCRH-BUTTON_H*2-BUTTON_D*2,BUTTON_W,BUTTON_H,0xF4F4F4,0xF4F4F4,0x289482,0x33C1A9,0,0xffffff,0);
    buttonCreate(&btn[17], "×", '*', BUTTON_D*4+BUTTON_W*3,SCRH-BUTTON_H*3-BUTTON_D*3,BUTTON_W,BUTTON_H,0xF4F4F4,0xF4F4F4,0x289482,0x33C1A9,0,0xffffff,0);
    buttonCreate(&btn[18], "÷", '/', BUTTON_D*4+BUTTON_W*3,SCRH-BUTTON_H*4-BUTTON_D*4,BUTTON_W,BUTTON_H,0xF4F4F4,0xF4F4F4,0x289482,0x33C1A9,0,0xffffff,0);
    buttonCreate(&btn[19], "AC",'C', BUTTON_D*4+BUTTON_W*3,SCRH-BUTTON_H*5-BUTTON_D*5,BUTTON_W,BUTTON_H,0xF4F4F4,0xF4F4F4,0xA8A8A8,0xDBDBDB,0,0,0);
    buttonShowAll(btn,BUTTON_N);
    textDisplay("");
    return 0;
}

//event函数，接收消息事件
int event(int type, int p1, int p2)
{
    buttonEvent(&btn, BUTTON_N, &buttonClickId,exp,EXP_LEN, type, p1, p2);
    if(KY_UP == type)
    {
        switch(p1)
        {
            case _BACK:
            exit();
            break;
            case _MENU:
            break;
        }
    }
    return 0;
}

//应用暂停，当程序进入后台或变为不可见时会调用此函数
int pause()
{
    return 0;
}

//应用恢复，程序变为可见时调用此函数
int resume()
{
    return 0;
}

